define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'statistics/order/index' + location.search,
                    add_url: 'statistics/order/add',
                    edit_url: 'statistics/order/edit',
                    del_url: 'statistics/order/del',
                    multi_url: 'statistics/order/multi',
                    import_url: 'statistics/order/import',
                    table: 'order',
                },
                pageSize: 15,
                pageList: [15],
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'schedule', title: __('Schedule'), operate:'BETWEEN'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')},
                            custom: {"1": "green",  "0": 'orange',"2": "red"},
                            formatter: Table.api.formatter.status},
                        {field: 'ddd', title: __('Operate'), table: table,events: Table.api.events.operate,formatter: Table.api.formatter.operate,
                            buttons: [
                                // {
                                //     name: 'addtabs',
                                //     text: __('查看'),
                                //     title: __('查看'),
                                //     classname: 'btn btn-xs btn-warning btn-click btn-addtabs',
                                //     icon: 'fa fa-folder-o',
                                //     url: 'statistics/orderdetail?ref=addtabs&order_no={order_no}',
                                // },
                                {
                                    name: 'detail',
                                    text: '查看',
                                    title: '查看',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-info btn-detail btn-dialog',
                                    url: 'statistics/orderdetail/index?'

                                },
                                // {
                                //     name: 'ajax',
                                //     text: __('发送Ajax'),
                                //     title: __('发送Ajax'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     icon: 'fa fa-magic',
                                //     url: 'example/bootstraptable/detail',
                                //     confirm: '确认发送',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                //         //如果需要阻止成功提示，则必须使用return false;
                                //         //return false;
                                //     },
                                //     error: function (data, ret) {
                                //         console.log(data, ret);
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     }
                                // },
                                {
                                    name: 'add_detail',
                                    text: '添加商品',
                                    title: '添加商品',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'statistics/order/add_detail'
                                }
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                            // events: Table.api.events.operate, formatter: this.actionFormatter}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // table.on("click",".btn-detail",function () {
            //        console.log('什么调换',$(this))
            // })

        },
        actionFormatter:function (value, row, index) {
            var id = row.order_no;
            var result = "";
            result += "<a href='/statistics/orderdetail?ref=addtabs' class='btn btn-xs green' title='查看'><span class='glyphicon glyphicon-search'>"+id+"</span></a>";
            result += "<a href='javascript:;' class='btn btn-xs blue' title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
            result += "<a href='javascript:;' class='btn btn-xs red' title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
            return result;

        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'statistics/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'statistics/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'statistics/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        add_detail: function () {
            Controller.api.bindevent();
        },
        show_detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'statistics/orderdetail/index' + location.search,
                    add_url: 'statistics/orderdetail/add',
                    edit_url: 'statistics/orderdetail/edit',
                    del_url: 'statistics/orderdetail/del',
                    multi_url: 'statistics/orderdetail/multi',
                    import_url: 'statistics/orderdetail/import',
                    table: 'order_detail',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                dblClickToEdit: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE'},
                        {field: 'trademark_printing', title: __('Trademark_printing'), operate: 'LIKE'},
                        {field: 'colour.name', title: __('色号'), operate: 'LIKE'},
                        // {field: 'abbreviation', title: __('Abbreviation'), operate: 'LIKE'},
                        {field: 'quantity', title: __('Quantity')},
                        // {field: 'colour_schedule', title: __('色号进度')},
                        {field: 'process_detail', title: __('工序详情'),formatter: function (value,row,index){
                                return btnGroup(value,row,index)
                            }},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams:function(params){
                    var filter=JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var order_no=Fast.api.query('order_no');
                    console.log('order_no',order_no)
                    if (order_no){
                        filter.order_no=order_no;
                        op.order_no='=';
                        params.filter=JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                        funcUrlDel('order_no');
                    }
                    return params;
                }
            });

            function btnGroup (value,row,index) {   // 自定义方法，添加操作按钮
                let url = 'statistics/orderdetail/process_detail?ids='+row.id
                let html = '<a href="'+url+'" class="btn btn-dialog" id="modRole" data-toggle="modal" data-target="#editrole" title="工序明细">' +
                    '详情</a>'
                return html
            };
            //删除url的id参数
            function funcUrlDel(name){
                var loca = window.location;
                var baseUrl = loca.origin + loca.pathname + "?";
                var query = loca.search.substr(1);

                if (query.indexOf(name)>-1) {
                    var obj = {};
                    var arr = query.split("&");
                    for (var i = 0; i < arr.length; i++) {
                        arr[i] = arr[i].split("=");
                        obj[arr[i][0]] = arr[i][1];
                    };
                    delete obj[name];
                    var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
                    history.pushState(null,null,url);
                    return;
                }else{
                    return window.location.href;
                };
            };

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
